#include "operationwidget.h"
#include "ui_operationwidget.h"
#include <QHostAddress>
#include <QDebug>
#include<QString>

operationWidget::operationWidget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::operationWidget)
{
    ui->setupUi(this);

    this->setWindowTitle("操作界面");

    ui->label_2->setPixmap(QPixmap(":/picture/control"));
    //setScaledContents:图片自适应控件大小
    ui->label_2->setScaledContents(true);
    //设置窗口大小不能改变
    this->setFixedSize(size());


    env_socket=new QTcpSocket(this);
    env_socket->connectToHost(ip,port);
    //connect(env_socket,SIGNAL(connected()),this,SLOT(net_connected()));
    connect(env_socket,SIGNAL(disconnected()),this,SLOT(net_disconnected()));
    connect(env_socket,SIGNAL(readyRead()),this,SLOT(recvData()));

    env_time=new QTimer(this);
    connect(env_time,SIGNAL(timeout()),this,SLOT(sendData()));
    env_time->start(1000);

}

operationWidget::~operationWidget()
{
    delete ui;
}
void operationWidget::net_disconnected()
{
    //isActive（）如果定时器正在运行，返回为真
    if(env_time->isActive())
        env_time->stop();
    env_socket->close();
}
void operationWidget::recvData()
{
    char buf[128];
    char string[6]={0};
    env_socket->read(buf,sizeof(buf));
   //int sscanf(const char *buffer,const char *format,[argument ]...);
   // buffer存储的数据  format格式控制字符串 argument 选择性设定字符串  sscanf会从buffer里读进数据，依照format的格式将数据写入到argument里。
    sscanf(buf,"%dt%dh%dl",&tmp,&hum,&light);
    //int snprintf(char *str, size_t size, const char *format, ...);
    //按照format格式最多将size个字符写入到str中，其中size个字符已经包括了结束符。
    snprintf(string,sizeof(string),"%d",tmp);
    ui->lineEdit->setText(string);
    snprintf(string,sizeof(string),"%d",hum);
    ui->lineEdit_2->setText(string);
    snprintf(string,sizeof(string),"%d",light);
    ui->lineEdit_3->setText(string);
}
void operationWidget::sendData()
{
    char buf[]="env";
    env_socket->write(buf,sizeof(buf));
    env_socket->flush();
}
void operationWidget::on_pushButton_clicked()//开关的按钮
{
    if(stoped )//开灯
        {
            ui->pushButton->setText("关灯");
            stoped=false;
            env_socket->write("Led_on",sizeof("Led_on"));
        }
        else//关灯
        {
            ui->pushButton->setText("开灯");
            stoped=true;
            env_socket->write("Led_off",sizeof("Led_off"));
        }
}

void operationWidget::on_pushButton_2_clicked()//开关警报按钮
{
    if(stoped)//打开警报
        {
            ui->pushButton_2->setText("关闭警报");
            stoped=false;
            env_socket->write("Buzzer_on",sizeof("Buzzer_on"));
        }
        else//关闭警报
        {
            ui->pushButton_2->setText("打开警报");
            stoped=true;
            env_socket->write("Buzzer_off",sizeof("Buzzer_off"));
        }
}

void operationWidget::on_pushButton_3_clicked()//开关排风按钮
{
    if(stoped)//打开排风
        {
            ui->pushButton_3->setText("关闭排风");
            stoped=false;
            env_socket->write("Fan_on",sizeof("Fan_on"));
        }
    else//关闭排风
        {
            ui->pushButton_3->setText("打开排风");
            stoped=true;
            env_socket->write("Fan_off",sizeof("Fan_off"));
        }
}

void operationWidget::on_pushButton_4_clicked()//打开监控按钮
{
        pic_socket=new QTcpSocket();
        connect(pic_socket,SIGNAL(disconnected()),this,SLOT(pic_disconnected()));
        connect(pic_socket,SIGNAL(readyRead()),this,SLOT(pic_recv()));
        pic_socket->connectToHost(ip,6969);
        pic_time=new QTimer(this);
        connect(pic_time,SIGNAL(timeout()),this,SLOT(pic_send()));
        pic_time->start(10);
}

void operationWidget::pic_disconnected()
{
    if(pic_time->isActive())
        pic_time->stop();
    pic_socket->close();
}
void operationWidget::pic_recv()
{
    switch (flag) {
    case 1: //接收大小
        piclen=pic_socket->read(8).toInt();
        //qDebug()<<"piclen:"<<piclen;
        flag=2;
        break;
    case 2:  //接收数据
        if(pic_socket->bytesAvailable() < piclen)//判断缓存区与长度的大小
            return;
        pic=pic_socket->read(piclen);
        //bool	loadFromData(const QByteArray & data, const char * format = 0, Qt::ImageConversionFlags flags = Qt::AutoColor)
        //根据内存中的图像数据进行导入，该函数就会根据QByteArray中数据进行解析，分析图像的格式等
        pix.loadFromData(pic,"JPEG");
        ui->label->setPixmap(pix);
        flag=1;
        break;
    }
}
void operationWidget::pic_send()
{
    char buf[]="pic";
    pic_socket->write(buf,sizeof(buf));
    pic_socket->flush();
}
void operationWidget::on_pushButton_5_clicked()//关闭监控
{
    if(pic_time->isActive())
        pic_time->stop();
    ui->label->clear();
    env_socket->write("p_close",sizeof("p_close"));
    pic_socket->close();
}



void operationWidget::on_pushButton_6_pressed()//语音控制按钮按下
{
    Audio =new audio;
    Audio->startAudio("D:\\1.pcm");

}

void operationWidget::on_pushButton_6_released()
{
    Audio->stopAudio();
    speech my_speech;
    QString text = my_speech.speechIdentify("D:\\1.pcm");
        if(text=="开灯。")//开灯
        {
            ui->pushButton->setText("关灯");
            stoped=false;
            env_socket->write("Led_on",sizeof("Led_on"));
        }
        else if(text=="关灯。")//关灯
        {
            ui->pushButton->setText("开灯");
            stoped=true;

            env_socket->write("Led_off",sizeof("Led_off"));
        }
         else if(text=="打开警报。")//打开警报
        {
            ui->pushButton_2->setText("关闭警报");
            stoped=false;
            env_socket->write("Buzzer_on",sizeof("Buzzer_on"));
        }
        else if(text=="关闭警报。")//关闭警报
        {
            ui->pushButton_2->setText("打开警报");
            stoped=true;
            env_socket->write("Buzzer_off",sizeof("Buzzer_off"));
        }
        else if(text=="打开排风。")//打开排风
        {
            ui->pushButton_3->setText("关闭排风");
            stoped=false;
            env_socket->write("Fan_on",sizeof("Fan_on"));
        }
        else if(text=="关闭排风。")//关闭排风
        {
            ui->pushButton_3->setText("打开排风");
            stoped=true;
            env_socket->write("Fan_off",sizeof("Fan_off"));
        }
        else if(text=="关闭监控")
        {
            on_pushButton_5_clicked();
        }
        else if(text=="打开监控")
        {
            on_pushButton_4_clicked();
        }
}

void operationWidget::on_pushButton_7_clicked()
{
    if(env_time->isActive())
        env_time->stop();
    if(env_socket!=nullptr)
        env_socket->close();
    if(pic_time->isActive())
        pic_time->stop();
    if(pic_socket!=nullptr)
        pic_socket->close();
    this->close();
}
